﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LibSolPedI;
using WebLibSolPedBE;
using SAP.Middleware.Connector;

namespace LibSolPedDALC
{
    public class DocSOCompraDALC : IDocSOCompra
    {
        private DocSOCompraDALC() { }
        private static volatile DocSOCompraDALC _instance = null;

        public static DocSOCompraDALC Instance()
        {
            if (_instance == null)
            {
                lock (typeof(DocSOCompraDALC))
                {
                    if (_instance == null)
                    {
                        _instance = new DocSOCompraDALC();
                    }
                }
            }
            return _instance;
        }

        public List<DocSOCompraBE> getLstDocSOCompra()
        {
            List<DocSOCompraBE> lst = new List<DocSOCompraBE>();

            IRfcFunction function = null;
            RfcDestination dest = null;
            DestConfig dc = new DestConfig();
            try
            {
                function = DALC.rfcRep.CreateFunction("Z_MOVIL_FI_GET_DOCSINOC_CAB");

                function.Invoke(DALC.rfcDest);
                IRfcTable T_RETORNO = function.GetTable("EP_TRETURN");
                foreach (IRfcStructure item in T_RETORNO)
                {
                    DocSOCompraBE it = new DocSOCompraBE();
                    it.BELNR = item["BELNR"].GetString();
                    it.BUKRS = item["BUKRS"].GetString();
                    it.GJAHR = item["GJAHR"].GetString();
                    it.BLART = item["BLART"].GetString();
                    it.DBLART = item["DBLART"].GetString();
                    it.BUDAT = item["BUDAT"].GetString();
                    it.LIFNR = item["LIFNR"].GetString();
                    it.DLIFNR = item["DLIFNR"].GetString();
                    it.WAERS = item["WAERS"].GetString();
                    it.WRBTR = item["WRBTR"].GetString();
                    it.XBLNR = item["XBLNR"].GetString();
                    it.KOSTL = item["KOSTL"].GetString();
                    it.DKOSTL = item["DKOSTL"].GetString();
                    it.VALOR1 = item["VALOR1"].GetString();
                    it.VALOR2 = item["VALOR2"].GetString();
                    it.VALOR3 = item["VALOR3"].GetString();
                    it.VALOR4 = item["VALOR4"].GetString();
                    it.VALOR5 = item["VALOR5"].GetString();
                    lst.Add(it);
                }
            }
            catch (Exception e)
            {

            }

            return lst;
        }

        public string setAprobarDocSOCompra(List<DocSOCompraBE> lst)
        {
            String ret = "Resultado de libetación: <br>" + Environment.NewLine;
            try
            {
                foreach (DocSOCompraBE it in lst)
                {
                    IRfcFunction function = null;
                    function = DALC.rfcRep.CreateFunction("Z_MOVIL_FI_APROB_DOC_SIN_OC");
                    function.SetValue("IP_BELNR", it.BELNR);
                    function.SetValue("IP_BUKRS", it.BUKRS);
                    function.SetValue("IP_GJAHR", it.GJAHR);
                    function.Invoke(DALC.rfcDest);
                    IRfcTable T_RETORNO = function.GetTable("TR_RETORNO");
                    foreach (IRfcStructure item in T_RETORNO)
                    {
                        ret = ret + "  -" + item["MESSAGE"].GetString() + ". <br>";
                    }
                    ret = ret + "<br>" + Environment.NewLine;
                }
            }
            catch (Exception ex)
            {
                ret = ret + "<br>" + "Error en el resto de documentos";
            }
            return ret;
        }
    }
}
